<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="员工id" prop="doctorId">
        <el-input
          v-model="queryParams.doctorId"
          placeholder="请输入员工id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="排班日期" prop="scheduleDate">
        <el-date-picker clearable
          v-model="queryParams.scheduleDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择排班日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="排班时间编号" prop="scheduleTimeId">
        <el-input
          v-model="queryParams.scheduleTimeId"
          placeholder="请输入排班时间编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扩展字段1" prop="ext1">
        <el-input
          v-model="queryParams.ext1"
          placeholder="请输入扩展字段1"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扩展字段2" prop="ext2">
        <el-input
          v-model="queryParams.ext2"
          placeholder="请输入扩展字段2"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扩展字段3" prop="ext3">
        <el-input
          v-model="queryParams.ext3"
          placeholder="请输入扩展字段3"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扩展字段4" prop="ext4">
        <el-input
          v-model="queryParams.ext4"
          placeholder="请输入扩展字段4"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扩展字段5" prop="ext5">
        <el-input
          v-model="queryParams.ext5"
          placeholder="请输入扩展字段5"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['gyf:paiban_management:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['gyf:paiban_management:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['gyf:paiban_management:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['gyf:paiban_management:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="paiban_managementList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="排班id" align="center" prop="scheduleId" />
      <el-table-column label="员工id" align="center" prop="doctorId" />
      <el-table-column label="员工名字" align="center" prop="name">
        <template #default="scope">
          <span>{{scope.row.employee.name}}</span>
        </template>
      </el-table-column>
      <el-table-column label="排班日期" align="center" prop="scheduleDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.scheduleDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="排班时间编号" align="center" prop="scheduleTimeDistribution">
        <template #default="scope">
          <span>{{scope.row.scheduleTimeDistribution.scheduleTimeId}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['gyf:paiban_management:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['gyf:paiban_management:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改医生排班对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="paiban_managementRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="员工id" prop="doctorId">
          <el-input v-model="form.doctorId" placeholder="请输入员工id" />
        </el-form-item>
        <el-form-item label="排班日期" prop="scheduleDate">
          <el-date-picker clearable
            v-model="form.scheduleDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择排班日期">
          </el-date-picker>
        </el-form-item>
<!--        <el-form-item label="排班时间编号" label-width="100px" prop="scheduleTimeId">-->
<!--          <el-input v-model="form.scheduleTimeId" placeholder="请输入排班时间编号" />-->
<!--        </el-form-item>-->
        <el-form-item label="可分配排班ID" label-width="100px" prop="scheduleTimeId">
          <el-select v-model="form.scheduleTimeId" placeholder="选择可分配排班时间ID" style="width: 240px" @click="test">
            <el-option
                v-for="item in paiban"
                :key="item.value"
                :label="item.label"
                :value="item.value"
                :disabled="item.disabled"
            />
          </el-select>

<!--          插入一条可分配的排班ID-->
          <el-button style="margin-left: 10px" :plain="true" type="success" @click="produceUseable">生成排班时间</el-button>
        </el-form-item>

      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Paiban_management">
import {
  listPaiban_management,
  getPaiban_management,
  delPaiban_management,
  addPaiban_management,
  updatePaiban_management,
  selectUseable, insertUseable, deleteScheduleTimeDistributionByid
} from "@/api/gyf/paiban_management";
import {parseTime} from "../../../utils/ruoyi.js";
import {ElMessage} from "element-plus";

const { proxy } = getCurrentInstance();

const paiban_managementList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const value = ref('')
const options = [
  {
    value: 'Option1',
    label: 'Option1',
  },
  {
    value: 'Option2',
    label: 'Option2',
    disabled: true,
  },
  {
    value: 'Option3',
    label: 'Option3',
  },
  {
    value: 'Option4',
    label: 'Option4',
  },
  {
    value: 'Option5',
    label: 'Option5',
  },
]

const paiban = ref([])
const test = ()=>{
  selectUseable().then(response => {
    const paiban2=response.data.map(item=>item);
     paiban.value = paiban2.map(item => ({
      value: item,
      label: item
    }));
    console.log(paiban.value);


  })
}

const produceUseable = ()=>{
  insertUseable().then(response => {
   if(response.code===200){
     ElMessage({
       message: '添加成功',
       type: 'success',
     })
   }else{
     ElMessage({
       message: '添加失败',
       type: 'error',
     })
   }
  })
}




const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    doctorId: null,
    scheduleDate: null,
    scheduleTimeId: null,
    ext1: null,
    ext2: null,
    ext3: null,
    ext4: null,
    ext5: null
  },
  rules: {
    doctorId: [
      { required: true, message: "员工id不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询医生排班列表 */
function getList() {
  loading.value = true;
    console.log("打印一下参数");
    console.log(queryParams.value)
  listPaiban_management(queryParams.value).then(response => {
    console.log(("成功拿到所有数据"))
    console.log(response);
    paiban_managementList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    scheduleId: null,
    doctorId: null,
    scheduleDate: null,
    scheduleTimeId: null,
    ext1: null,
    ext2: null,
    ext3: null,
    ext4: null,
    ext5: null
  };
  proxy.resetForm("paiban_managementRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.scheduleId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加医生排班";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _scheduleId = row.scheduleId || ids.value
  getPaiban_management(_scheduleId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改医生排班";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["paiban_managementRef"].validate(valid => {
    if (valid) {
      if (form.value.scheduleId != null) {
        updatePaiban_management(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addPaiban_management(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  // console.log("打印一下row")
  // console.log(row.scheduleTimeDistribution.scheduleTimeId
  // )
  const _scheduleIds = row.scheduleId || ids.value;
  proxy.$modal.confirm('是否确认删除医生排班编号为"' + _scheduleIds + '"的数据项？').then(function() {
    deleteScheduleTimeDistributionByid(row.scheduleTimeDistribution.scheduleTimeId)
    return delPaiban_management(_scheduleIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});


  // //通过时间排班id删除时间排班信息
  // deleteScheduleTimeDistributionByid(row.scheduleTimeDistribution.scheduleTimeId)
  //     .then(response => {
  //       console.log("成功通过id删除时间排班id")
  //       console.log(response);
  //     })

}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('gyf/paiban_management/export', {
    ...queryParams.value
  }, `paiban_management_${new Date().getTime()}.xlsx`)
}

getList();
</script>
